/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2014-08-25 11:28:23
 * @filename : sea-modules/panel/zepto.panel.js
 * @version : 
 * @description : 
 */
(function(){
 
  var navW = 260,userInfo,nav='userNav';  

  $(window).bind('orientationchange',function(){
        setTimeout(function() {
          $.fn.panel.switchRun();
        },500);
  });
 
  $.fn.panel = {
    
        info : {navW:260, state :  $(window).width() < 640 ? 'hidden' : 'show', callback:function(){} },
    
        init : function() {
            var tthis = this;
            userInfo = share.userInfo();
            $('#userNav').css('display','none'); 
            $('#userNav').find('.menu').on('singleTap',function(){
                var url = $(this).attr('url');
                if (url) {
                   window.location.href='#'+url+'/whole'; 
                }
            }).on('touchmove',function(e){
                if ($(window).height() > 375) {
                   e.preventDefault();
                   e.stopPropagation();
                }
            });

            $('#userNav').find('.userNav_bgClear').on('tap',function(){
                tthis.hidden();     
            });

            $('#userNav').find('.userNav_des_icon').on('tap',function(){
                if ($(window).width() < 640) {
                    setTimeout(function() {
                        tthis.hidden();
                    },200);
                }
                window.location.href='#myProfile/whole'; 
            });

            $('#userNav').find('.userNav_des_text').on('tap',function(){
                if ($(window).width() < 640) {
                    setTimeout(function() {
                        tthis.hidden();
                    },200);
                }
                window.location.href='#myProfile/whole'; 
            });

            this.info.navDom = $('#userNav');
            this.renderUserInfo();
        },

       run : function(opt) {
           var tthis = this;

           for (var i in opt ) {
               this.info[i] = opt[i];
           }
          
 
           if (opt.navName !== '' || opt.navName !== undefined) {
               if (this.info.navDom.find('li[nav='+opt.navName+']').length > 0) {
                   this.info.navDom.find('li').removeClass('action');
                   this.info.navDom.find('li[nav='+opt.navName+']').addClass('action');
               } else if (opt.navName == 'myProfile') {
                   this.info.navDom.find('li').removeClass('action');
               }
           }
                       
           if ($(window).width() < 640) {
              this.info.rootDom.find('.body_all').css({'min-width':$(window).width()+'px'});
              setTimeout(function() {
                  tthis.hidden();
              },200);
           } else {
              tthis.switchRun();
           }
           
       },
                     
       renderUserInfo : function() {

            if (userInfo == null) {
                share.loginOut(); 
            }

            var pic = '',
                temp = share.getObjStorage(userInfo.usr_id),
                obj = (temp !== null) ? temp : userInfo;
 
            if (obj.pictures !== undefined && obj.pictures.length > 0) {
                pic = obj.pictures[0].picture;
            } 

            if (obj.status.isGuest == 0) {
               this.info.navDom.find('li[nav="upgrade"]').hide();
            }

            var userName = obj.account.username,
                age =obj.account.age,
                stateName = obj.account.state_name,
                city = obj.account.city == null ? stateName : obj.account.city,               
                countryName = obj.account.country_name,
                income_verify = obj.status.income_verify,
                isGuest = obj.status.isGuest, 
                gender =  obj.account.gender; 
            
            $('#userNav').find('.userNav_des_icon').find('img').attr('url',this.defalutImg(gender,share.handleVal(pic))).addClass('imglazyload');
            $('#userNav').find('.userNav_name').find('span').first().html(share.handleVal(userName));
            $('#userNav').find('.userNav_age').find('span').first().html(obj.account !== undefined ? share.handleVal(age) : share.age(parseInt(age)));
            $('#userNav').find('.userNav_age').find('span').eq(2).html(share.handleVal(city));
            $('#userNav').find('.userNav_age').find('span').eq(4).html(share.handleVal(countryName));
            if (!parseInt(income_verify)) {
                $('#userNav').find('.userNav_status').find(".diamond").removeAttr('class');
            }
           
            if (parseInt(isGuest)) {
                $('#userNav').find('.userNav_status').find(".gold").removeAttr('class');
            }

            $('#userNav').imglazyload({startY : 0,endY: $(window).height(),sW:50,sH:50}); 
       },

       defalutImg : function(gender,img) {
            return share.defalutImg(gender,img); 
       },

       switchRun : function() {     
           if (this.info.state == 'hidden') {
               this.hidden();
           } else {
               this.show();
           }
       },
 
       hidden : function() { 
           this.info.state = 'hidden';
           if ($(window).width() < 640) {
               this.hiddenLess640();
           } else {
               this.hiddenThan640();
           }  
       },

       show : function() {
           this.renderUserInfo();
           this.info.state = 'show';
           if ($(window).width() < 640) {
               this.showLess640();
           } else {
               this.showThan640();
           } 
       },
        
       hiddenLess640 : function() {
           var tthis = this;
           this.transformCSS(this.info.navDom,-navW,200);
           this.transformCSS(this.info.rootDom,0,200);
           this.info.navDom.css({
                width:navW+'px'
           }).find('.userNav_bgClear').hide();

           this.info.rootDom.children().each(function(){
                $(this).css('width','100%');
                tthis.transformCSS($(this),0,200);
           });            
           this.info.rootDom.off('touchmove').css({
                 'width' : '100%',
                 'overflow-x':'visible'           
           });
           this.callback();
       },

       showLess640 : function() {
           var tthis = this;
           this.transformCSS(this.info.navDom,0,200);
           this.transformCSS(this.info.rootDom,navW,200);
   
           this.info.rootDom.children().each(function(){
                $(this).css('width','100%');
                tthis.transformCSS($(this),navW,200);
           });

           this.info.navDom.on('touchmove',function(e){
                  if ($(window).height() > 400) {
                      e.stopPropagation(); 
                      e.preventDefault(); 
                  }
           }).css({
                width:'100%'
           }).find('.userNav_bgClear').show();

           this.info.rootDom.on('touchmove',function(e){ 
                  if ($(window).width() < 640) {
                      e.stopPropagation(); 
                      e.preventDefault(); 
                  }
           }).css({
                 'width' : ($(window).width() - navW)+'px',
                 'overflow':'hidden'
           });;
           this.callback();
       },
    
       hiddenThan640 : function() {
           var tthis = this,
               contentW = $(window).width();
          
               this.info.rootDom.css({width:contentW+'px'});
               this.info.contentDom.css({width:contentW+'px'});
               this.info.rootDom.find('.head').css({width:contentW+'px'});

           setTimeout(function() {
               tthis.transformCSS(tthis.info.navDom,-navW,200);
               tthis.transformCSS(tthis.info.rootDom,0,200);
               tthis.transformCSS(tthis.info.contentDom,0,200);
               tthis.transformCSS(tthis.info.rootDom.find('.head'),0,0);
           },200);
           this.callback();
       },

       showThan640 : function() {
           var tthis = this;
           this.transformCSS(this.info.navDom,0,200);
           this.transformCSS(this.info.rootDom,navW,200);
           this.transformCSS(this.info.contentDom,navW,200);
           this.transformCSS(this.info.rootDom.find('.head'),navW,0);
           this.info.navDom.css({
                width:navW+'px'
           }).find('.userNav_bgClear').hide();


           setTimeout(function() {
                var contentW = $(window).width() - navW;
                tthis.info.rootDom.css({width:contentW+'px'});
                tthis.info.contentDom.css({width:contentW+'px'});
                tthis.info.rootDom.find('.head').css({width:contentW+'px'});
           },200);

           this.callback();
       },
        
       transformCSS : function(dom,move,time) {
           dom.css({
                   'left': move+'px',
                   'display':'block',
                   'transition': time+'ms linear',
                   '-webkit-transition': time+'ms linear', 
                   '-moz-transition': time+'ms linear'
           });
       },

       callback : function() {
           this.info.callback(this.info);
       }
    
    };


    if (typeof define === "function" ) {
        define("panel/zepto.panel.js", [], function () { return $.fn.panel;} );
    }

})();

